package vn.edu.saigontech.ncms.service.impl;

import java.util.Collection;
import java.util.Date;

import org.hibernate.Criteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import com.opensymphony.xwork2.ActionContext;

import vn.edu.saigontech.ncms.model.enumeration.Gender;
import vn.edu.saigontech.ncms.model.info.Student;
import vn.edu.saigontech.ncms.model.info.StudentType;
import vn.edu.saigontech.ncms.model.user.Site;
import vn.edu.saigontech.ncms.service.EducationService;
import vn.edu.saigontech.ncms.service.StudentTypeService;
import vn.edu.saigontech.ncms.util.HibernateUtil;
import vn.edu.saigontech.ncms.util.Checker;

public class EducationServiceImpl implements EducationService {

	@SuppressWarnings("unchecked")
	public Collection<Student> findByFirstName(String fullName, int type) {
		Criteria criteria = HibernateUtil.getCurrentSession().createCriteria(Student.class);
		criteria.add(Restrictions.eq("type", this.getStudentStype()));
		if(!Checker.isEmpty(fullName)) {
			criteria.add(Restrictions.ilike("firstName", fullName, MatchMode.ANYWHERE));
		}
		if(type == 1) {
			criteria.add(Restrictions.isNotEmpty("offeringReceipts"));
		}
		else if(type == 2) {
			criteria.add(Restrictions.isEmpty("offeringReceipts"));
		}
		criteria.addOrder(Order.asc("firstName"));
		return criteria.list();
	}

	@SuppressWarnings("unchecked")
	public Collection<Student> findStudents() {
		Criteria criteria = HibernateUtil.getCurrentSession().createCriteria(Student.class);
		criteria.add(Restrictions.eq("type", this.getStudentStype()));
		criteria.addOrder(Order.asc("firstName"));
		return criteria.list();
	}

	@SuppressWarnings("unchecked")
	public Collection<Student> findStudents(String firstName, String lastName, Gender gender, Date dateOfBirth, Long levelId, Long offeringId, String receiptNo) {
		Criteria criteria = HibernateUtil.getCurrentSession().createCriteria(Student.class);
		criteria.add(Restrictions.eq("type", this.getStudentStype()));
		if(!Checker.isEmpty(firstName)) {
			criteria.add(Restrictions.ilike("firstName", firstName, MatchMode.ANYWHERE));
		}
		if(!Checker.isEmpty(lastName)) {
			criteria.add(Restrictions.ilike("lastName", lastName, MatchMode.ANYWHERE));
		}
		if(!Checker.isNull(gender)) {
			criteria.add(Restrictions.eq("gender", gender));
		}
		if(!Checker.isNull(dateOfBirth)) {
			criteria.add(Restrictions.eq("dateOfBirth", dateOfBirth));
		}
		if(!Checker.isEmpty(receiptNo) || !Checker.isNull(offeringId) || !Checker.isNull(levelId)) {
			Criteria receiptCriteria = criteria.createCriteria("offeringReceipts", "receipt");
			if(!Checker.isEmpty(receiptNo)) {
				receiptCriteria.add(Restrictions.like("number", receiptNo, MatchMode.ANYWHERE));
			}
			Criteria offeringCriteria = receiptCriteria.createCriteria("offering");
			if(!Checker.isNull(offeringId)) {
				offeringCriteria.add(Restrictions.idEq(offeringId));
			}
			if(!Checker.isNull(levelId)) {
				offeringCriteria.createCriteria("level").add(Restrictions.idEq(levelId));
			}
		}
		criteria.addOrder(Order.asc("firstName"));
		return criteria.list();
	}
	
	private StudentType getStudentStype() {
		Site site = (Site) ActionContext.getContext().getSession().get("site");
		return StudentTypeService.INSTANCE.findByName(site.getName());
	}
}